//
//  WXPKHepler.m
//  战队卡牌
//
//  Created by tianlong on 2018/4/3.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "WXPKHepler.h"
#import <Lottie/Lottie.h>

// 战队卡牌UI设计宽度
#define WXPKCard_UI_Width 295
// 战队卡牌UI设计高度
#define WXPKCard_UI_Height 427


@implementation WXPKHepler

+ (CGFloat)referScale{
    // 战队卡牌缩放比例参考标准
    CGFloat pptW = [LiveSizeConfig getPPTWidth];
    CGFloat pptH = [LiveSizeConfig getPPTHeight];
    CGFloat scaleW = WXPKCard_UI_Width/pptW;
    CGFloat scaleH = WXPKCard_UI_Height/pptH;
    
    // 参考标准
    CGFloat normScale = scaleW < scaleH ? scaleW : scaleH;
    
    // UI设计的高度 大于 PPT的高度 则缩放到ppt的高度 *  ppt/
    normScale = WXPKCard_UI_Height > pptH ? (pptH/WXPKCard_UI_Height) : 1;
    return normScale;
}


/**
 返回适配后的控件的尺寸
 @param uiSize UI设计好的子控件尺寸
 @param referSize UI设计时参考的父视图的尺寸
 */
+ (CGSize)getLayoutSizeFromUISize:(CGSize)uiSize referSize:(CGSize)referSize {
    
    // UI设计时参考的父视图的尺寸，以直播间为例，参考的父视图的设计标准是（500，375）
    CGSize size = uiSize;
    if (referSize.height > 0 && referSize.width > 0) {
        CGFloat sw = uiSize.width*[WXPKHepler layoutWidthScale];
        CGFloat sh = uiSize.height*[WXPKHepler layoutHeightScale];
        size = CGSizeMake(sw, sh);
        return size;
    }
    return size;
}

+ (CGSize)getLayoutSizeFromUISize:(CGSize)uiSize{
    return [self getLayoutSizeFromUISize:uiSize referSize:WXPKTeamCardReferSize];
}

#pragma mark - 适配比例
+ (CGFloat)getScaleWithReferHeight:(CGFloat)referHeight{
    CGFloat ppth = [LiveSizeConfig getPPTHeight];
    CGFloat scale = 1;
    if (referHeight > 0) {
        scale = ppth / referHeight;
    }
    return scale;
}

+ (CGFloat)getScaleWithReferWidth:(CGFloat)referWidth{
    CGFloat pptw = [LiveSizeConfig getPPTWidth];
    CGFloat scale = 1;
    if (referWidth > 0) {
        scale = pptw / referWidth;
    }
    return scale;
}

/** 宽度适配比例 = 高度适配比例 */
+ (CGFloat)layoutWidthScale{
    return [WXPKHepler getScaleWithReferWidth:WXPKTeamCardReferWidth];
}
/** 高度适配比例 = 宽度适配比例 */
+ (CGFloat)layoutHeightScale{
    return [WXPKHepler getScaleWithReferHeight:WXPKTeamCardReferHeight];
}

#pragma mark - 适配动画用的比例
+ (CGFloat)lotSizeScaleWithView:(UIView *)view{
    // 如果其父视图是LOTView的类型，则返回相应的系数比例
    if ([view.superview isMemberOfClass:[LOTView class]]) {
        return 2;
    }
    return 0.5;
}

+ (CGFloat)lotMarginScaleWithView:(UIView *)view{
    // 如果其父视图是LOTView的类型，则返回相应的系数比例
    if ([view.superview isMemberOfClass:[LOTView class]]) {
        return 1;
    }
    return 0.5;
}

+ (CGFloat)layoutSizeScaleForType:(WXPKLayoutScaleType)type{
    if (type == WXPKLayoutScaleTypeNormal) {
        return 0.5;
    }else if (type == WXPKLayoutScaleTypeLottie){
        return 2;
    }
    return 0.5;
}

+ (CGFloat)layoutMarginScaleForType:(WXPKLayoutScaleType)type{
    if (type == WXPKLayoutScaleTypeNormal) {
        return 0.5;
    }else if (type == WXPKLayoutScaleTypeLottie){
        return 1;
    }
    return 0.5;
}

+ (NSString *)toJson:(NSDictionary *)dict{
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString;
    if (!jsonData) {
        NSLog(@"%@",error);
    }else{
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    NSRange range = {0,jsonString.length};
    //去掉字符串中的空格
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    return mutStr;
}

@end
